package cn.yotta.user.core.web.controller.common;

import cn.yotta.user.api.params.user.UserFindByTokenRequest;
import cn.yotta.user.core.server.service.LoginService;
import cn.yotta.user.core.web.iface.TokenStorageStrategy;
import cn.yotta.user.core.web.utils.ApiExecutor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wyaoyao
 * @date 2022/11/8 9:51
 */
@Slf4j
@Api("用户基础登录服务")
@RestController
@RequestMapping("user/common/logout")
public class LogoutController {

    private final LoginService loginService;
    private final TokenStorageStrategy tokenStorageStrategy;

    public LogoutController(LoginService loginService, TokenStorageStrategy tokenStorageStrategy) {
        this.loginService = loginService;
        this.tokenStorageStrategy = tokenStorageStrategy;
    }


    @ApiOperation(value = "用户登出")
    @GetMapping()
    public Boolean logout(HttpServletRequest httpServletRequest) {
        // 获取token
        String token = this.tokenStorageStrategy.getToken(httpServletRequest);
        UserFindByTokenRequest request = new UserFindByTokenRequest();
        request.setToken(token);
        // 执行登出逻辑
        ApiExecutor.execute(loginService::logout, request);
        // 删除token
        tokenStorageStrategy.remove();
        return Boolean.TRUE;
    }


}
